/**
 * Created by jiachenpan on 16/11/18.
 */

export function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if (typeof (time) === 'string') {
      time = new Date(time.replace(/-/g, '/'))
    }
    if (typeof (time) === 'number' && ('' + time).length === 10) {
      time = parseInt(time) * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}

export function formatTime(time, option) {
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if (typeof (time) === 'string') {
      time = new Date(time.replace(/-/g, '/'))
    }
    if (typeof (time) === 'number' && ('' + time).length === 10) {
      time = parseInt(time) * 1000
    }
    date = new Date(time)
  }
  // time = +time * 1000
  const d = date
  const now = Date.now()

  const diff = (now - d) / 1000

  if (diff < 30) {
    return '刚刚'
  } else if (diff < 3600) {
    // less 1 hour
    return Math.ceil(diff / 60) + '分钟前'
  } else if (diff < 3600 * 24) {
    return Math.ceil(diff / 3600) + '小时前'
  } else if (diff < 3600 * 24 * 2) {
    return '1天前'
  }
  if (option) {
    return parseTime(time, option)
  } else {
    return (
      d.getMonth() +
      1 +
      '月' +
      d.getDate() +
      '日' +
      d.getHours() +
      '时' +
      d.getMinutes() +
      '分'
    )
  }
}

export function getLines(str) {
  if (!str) return null
  str = str.replace(/\r\n/g, '\n')
  return str.split('\n')
}

export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

export function cleanArray(actual) {
  const newArray = []
  for (let i = 0; i < actual.length; i++) {
    if (actual[i]) {
      newArray.push(actual[i])
    }
  }
  return newArray
}
export function param(json) {
  if (!json) return ''
  return cleanArray(
    Object.keys(json).map(key => {
      if (json[key] === undefined) return ''
      return encodeURIComponent(key) + '=' + encodeURIComponent(json[key])
    })
  ).join('&')
}

export function param2Obj(url) {
  const search = url.split('?')[1]
  if (!search) {
    return {}
  }
  return JSON.parse(
    '{"' +
      decodeURIComponent(search)
        .replace(/"/g, '\\"')
        .replace(/&/g, '","')
        .replace(/=/g, '":"') +
      '"}'
  )
}
// 判断参数是否是其中之一
export function oneOf(value, validList) {
  for (let i = 0; i < validList.length; i++) {
    if (value === validList[i]) {
      return true
    }
  }
  return false
}

export function canAdd(state, controllerName) {
  const operateList = state.user.operateList
  if (!operateList) return true
  if (!controllerName) {
    controllerName = state.app.currentPageName
  }
  if (!operateList) return false
  if (operateList.length === 0) return false
  let hasPermission = false
  for (const i in operateList) {
    if (operateList[i].Controller.toLowerCase() === controllerName.toLowerCase() && operateList[i].OperateType === 'create') {
      hasPermission = true
      break
    }
  }
  return hasPermission
}

export function canUpdate(state, controllerName) {
  const operateList = state.user.operateList
  if (!operateList) return true
  if (!controllerName) {
    controllerName = state.app.currentPageName
  }
  if (!operateList) return false
  if (operateList.length === 0) return false
  let hasPermission = false
  for (const i in operateList) {
    if (operateList[i].Controller.toLowerCase() === controllerName.toLowerCase() && operateList[i].OperateType === 'update') {
      hasPermission = true
      break
    }
  }
  return hasPermission
}

export function canSubmit(state, controllerName) {
  const operateList = state.user.operateList
  if (!operateList) return true
  if (!controllerName) {
    controllerName = state.app.currentPageName
  }
  if (!operateList) return false
  if (operateList.length === 0) return false
  let hasPermission = false
  for (const i in operateList) {
    if (operateList[i].Controller.toLowerCase() === controllerName.toLowerCase() && operateList[i].OperateType === 'submit') {
      hasPermission = true
      break
    }
  }
  return hasPermission
}

export function canVerify(state, controllerName) {
  const operateList = state.user.operateList
  if (!operateList) return true
  if (!controllerName) {
    controllerName = state.app.currentPageName
  }
  if (!operateList) return false
  if (operateList.length === 0) return false
  let hasPermission = false
  for (const i in operateList) {
    if (operateList[i].Controller.toLowerCase() === controllerName.toLowerCase() && operateList[i].OperateType === 'verify') {
      hasPermission = true
      break
    }
  }
  return hasPermission
}

export function canDelete(state, controllerName) {
  const operateList = state.user.operateList
  if (!operateList) return false
  if (!controllerName) {
    controllerName = state.app.currentPageName
  }
  if (!operateList) return false
  if (operateList.length === 0) return false
  let hasPermission = false
  for (const i in operateList) {
    if (operateList[i].Controller.toLowerCase() === controllerName.toLowerCase() && operateList[i].OperateType === 'delete') {
      hasPermission = true
      break
    }
  }
  return hasPermission
}
